package com.autosalon.DaoCore;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment;

import com.autosalon.model.Adresa;
import com.autosalon.model.Automobil;
import com.autosalon.model.Banka;
import com.autosalon.model.Doplnek;
import com.autosalon.model.Financovani;
import com.autosalon.model.Kategorie;
import com.autosalon.model.Model;
import com.autosalon.model.Objednavka;
import com.autosalon.model.Poisteni;
import com.autosalon.model.Poistovna;
import com.autosalon.model.Pujcka;
import com.autosalon.model.Role;
import com.autosalon.model.StavFinancovani;
import com.autosalon.model.StavObjednavky;
import com.autosalon.model.Vyrobce;
import com.autosalon.model.Zakaznik;


public final class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {

        try {
            AnnotationConfiguration cnf = new AnnotationConfiguration();
            cnf.setProperty(Environment.DRIVER, com.microsoft.sqlserver.jdbc.SQLServerDriver.class.getName());
            cnf.setProperty(Environment.URL, "jdbc:sqlserver://databaze.cs.vsb.cz\\amalkaserver2008");
            cnf.setProperty(Environment.USER, "tym04_2011");
            cnf.setProperty(Environment.PASS, "picaso2");
            cnf.setProperty(Environment.DIALECT, org.hibernate.dialect.SQLServerDialect.class.getName());
            cnf.setProperty(Environment.SHOW_SQL, "false");
            cnf.setProperty(Environment.HBM2DDL_AUTO, "validate");
//            cnf.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
            cnf.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            cnf.addAnnotatedClass(Zakaznik.class);
            cnf.addAnnotatedClass(Objednavka.class);
            cnf.addAnnotatedClass(Adresa.class);
            cnf.addAnnotatedClass(Model.class);
            cnf.addAnnotatedClass(Vyrobce.class);
            cnf.addAnnotatedClass(Kategorie.class);
            cnf.addAnnotatedClass(Automobil.class);
            cnf.addAnnotatedClass(Doplnek.class);
            cnf.addAnnotatedClass(Financovani.class);
            cnf.addAnnotatedClass(StavFinancovani.class);
            cnf.addAnnotatedClass(Poistovna.class);
            cnf.addAnnotatedClass(Poisteni.class);
            cnf.addAnnotatedClass(Pujcka.class);
            cnf.addAnnotatedClass(Banka.class);
            cnf.addAnnotatedClass(Role.class);
            cnf.addAnnotatedClass(StavObjednavky.class);
            sessionFactory = cnf.buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private HibernateUtil() {
    }
}
